<?php

namespace Admin\Controller;
use Think\Controller;
class PublicController extends Controller {
    
    public function register(){
        if(IS_POST){
            $User = D('User');
            
            $post = I('post.');
            if($User->create()){
                $User->add();
                $this->success('注册成功，正在跳转到登陆界面', U('Public/login'));
            }  else {
                $this->error($User->getError());
            }
        }  else {
            $this->display();
        }
    }
    
    //用户登录
    public function login(){
        if(IS_POST){
            $User = D("User");
            
            $uid = $User->login(I('post.username'), I('post.password'));
            if($uid > 0){
                $this->success('登陆成功',U('Index/index'));
            }  else {
                switch($uid) {
                        case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
                        case -2: $error = '密码错误！'; break;
                        default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
                }
                $this->error($error);                
            }
        }else{
            $this->display();
        }
    }
    //用户登出
    public function logout(){
        if(is_login()){
            session('[destroy]');
            $this->success('退出成功！', U('login'));
        } else {
            $this->redirect('login');
        }
    }
}

